package apptoko.com.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import apptoko.com.ItemBrokerActivity;
import apptoko.com.adapter.RelatedItemAdapter;
import apptoko.com.model.RelatedItems;
import apptoko.com.model.ContentItemInfo.RelatedItem;
import apptoko.com.util.Constants;

import com.androidquery.AQuery;
import apptoko.com.R;
import com.google.analytics.tracking.android.EasyTracker;

import de.greenrobot.event.EventBus;

public class RelatedAppsFragment extends BaseFragment implements  OnItemClickListener{
	
	private AQuery aq;
//	private String appId;
	private ListView listView;
	private EventBus eventBus;
	public static int REVIEW_RETURN_CODE=1312;
	private RelatedItemAdapter adapter;
	private RelatedItems item;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mParent = inflater.inflate(R.layout.related_apps, null);
		aq = new AQuery(mParent);
		listView = (ListView) mParent.findViewById(R.id.related_list);
		aq.id(R.id.related_list).adapter(adapter).itemClicked(this);
		aq.id(listView).adapter(adapter);
		if(item.getRelatedApplications().size()==0){
			aq.id(R.id.dummy_related).text(getResources().getString(R.string.no_content));
		}
		return mParent;
	}

	@Override
	protected void initVariables() {
		// TODO Auto-generated method stub
		EasyTracker.getInstance().setContext(mContext);
		eventBus = EventBus.getDefault();
		item = (RelatedItems) getArguments().getSerializable(Constants.ITEM_INFO);
		adapter = new RelatedItemAdapter(mContext, R.layout.related_item, item.getRelatedApplications());
	}

	@Override
	protected void initActions() {
		// TODO Auto-generated method stub
//		bgTask.getComments(Utils.getStaticAccessToken(getActivity()), appId, START);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View v, int pos, long arg3) {
		// TODO Auto-generated method stub
		
		RelatedItem item = (RelatedItem) parent.getItemAtPosition(pos);
		EasyTracker.getTracker().sendEvent(Constants.EVENT, "Click Related App", "Click Related App "+item.getName(), Long.valueOf(1));
		Intent i = new Intent(getActivity(), ItemBrokerActivity.class);
		i.putExtra(Constants.SLUG, item.getSlug());
		i.putExtra(Constants.PREVIOUS_SCREEN, "/Details");
		i.putExtra(Constants.KEY_REFERER, "related");
		i.putExtra(Constants.STORE_KEY, getArguments().getString(Constants.STORE_KEY));
		i.putExtra(Constants.FROM, getArguments().getInt(Constants.FROM));
		startActivity(i);
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
}
